package com.qf.controller;

import com.qf.pojo.UserPojo;
import com.qf.service.UserPojoService;
import com.qf.service.UserService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    UserPojoService userPojoService;

    //新增员工
    @PostMapping("/add")
    @ResponseBody
    public ResultMsg add(@RequestBody UserPojo userPojo) {
        return userPojoService.add(userPojo);
    }

    //修改员工密码
    @PostMapping("/updatePass1")
    public ResultMsg updatePass(@RequestBody Map<String, Object> map) {
        return userPojoService.updatePass((Integer) map.get("userId"),(String) map.get("userPass"));
    }

    //根据用户id删除用户
    @GetMapping("/delById/{userId}")
    public ResultMsg delById(@PathVariable Integer userId) {
        return userPojoService.delById(userId);
    }

    //查询员工姓名
    @GetMapping("/findAll")
    public ResultMsg findAll(String keyword) {
        return userPojoService.findAll(keyword);
    }

    @PostMapping("/updateInfo")
    @ResponseBody
    public ResultMsg updateInfo(@RequestBody UserPojo userPojo) {
        return userPojoService.updateInfo(userPojo);
    }

    @PostMapping("/login")
    public ResultMsg login(@RequestBody Map<String, String> map, HttpServletRequest request) {
        return userService.login(map.get("userName"), map.get("userPass"), request);
    }

    @PostMapping("/updatePass2")
    public ResultMsg updatePass(@RequestBody Map<String, String> map, HttpServletRequest request) {
        return userService.updateUserPass(map.get("oldPass"), map.get("newPass"), request);
    }

    @GetMapping("/findOne/{userId}")
    public ResultMsg findOne(@PathVariable("userId") Integer userId) {
        return userService.queryOneUser(userId);
    }

    @PostMapping("/UpdateUser")
    public ResultMsg updateUser(@RequestBody UserPojo user) {
        return userService.updateUser(user);
    }

    @PostMapping("/logout")
    public ResultMsg logout(HttpServletRequest request) {
        return userService.logout(request);
    }

    @GetMapping("/getAllDoctor")
    public ResultMsg getAllDoctor() {
        return userService.findAllDoctor();
    }
}
